package com.file.fileService.controller;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.UUID;

@Controller
@RequestMapping("/file/")
public class FileController {

    @RequestMapping("uploadPage")
    public String upload() {
        return "upload";
    }

    @RequestMapping("upload")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file) {
        String fileName = file.getOriginalFilename();
        System.out.println(fileName);
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        String filePath = "d:/upload/";
        String uuid = UUID.randomUUID().toString().replace("-", "");
        fileName = filePath + uuid + suffixName;
        File dest = new File(fileName);
        // 判断路径是否存在，如果不存在则创建
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            // 保存到服务器中
            file.transferTo(dest);
            return uuid + suffixName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "上传失败";
    }

    @RequestMapping("download")
    public void download(HttpServletResponse response, String fileName) {
        try {
            // 文件地址，真实环境是存放在数据库中的
            File file = new File("D:\\upload\\" + fileName);
            // 穿件输入对象
            FileInputStream fis = new FileInputStream(file);
            // 设置相关格式
            response.setContentType("application/force-download");
            // 设置下载后的文件名以及header
            response.addHeader("Content-disposition", "attachment;fileName=" + fileName);
            // 创建输出对象
            OutputStream os = response.getOutputStream();
            // 常规操作
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = fis.read(buf)) != -1) {
                os.write(buf, 0, len);
            }
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("downloadTwo")
    public void downloadTwo(HttpServletResponse response, String fileName) {
        try {
            // 文件地址，真实环境是存放在数据库中的
            File file = new File("D:\\upload\\" + fileName);
            // 穿件输入对象
            FileInputStream inputStream = new FileInputStream(file);
            OutputStream outputStream = response.getOutputStream();
            // 设置相关格式
            response.setContentType("application/x-download");
            // 设置下载后的文件名以及header
            response.addHeader("Content-disposition", "attachment;fileName=" + fileName);
            //把输入流copy到输出流
            IOUtils.copy(inputStream, outputStream);
            outputStream.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
